#!/bin/bash

# -------------------------------------------------------------------------------------
# 检查是否添加Docker网络
# -------------------------------------------------------------------------------------
checkNetwork() {
  is_add=$(docker network ls | grep 'database_app' | awk '{print $1}')
  if [ -z "$is_add" ]; then
    docker network create database_app
  fi
}

# -------------------------------------------------------------------------------------
# 帮助函数
# -------------------------------------------------------------------------------------
help() {
    echo "database [option]"
    echo "  start       运行容器"
    echo "  restart     重启容器"
    echo "  stop        停止容器"
    echo "  remove      删除容器"
    echo "  help        帮助信息"
}

# 执行操作
ACTION=$1

case $ACTION in
    start)
        checkNetwork
        docker-compose up -d
        exit 1
    ;;
    restart)
        checkNetwork
        docker-compose restart
        exit 1
    ;;
    stop)
       docker-compose stop
       exit 1
    ;;
    remove)
        docker-compose stop
        docker-compose rm
        exit 1
    ;;
    help)
        help
        exit 1
    ;;
    *)
        echo "Unknown option."
        help
        exit 1
    ;;
esac